/*
 * DBuscar.java
 *
 * Created on 16 de mayo de 2006, 03:18 PM
 */

package ig.metrica.ui.seguridad;

import ejb.metrica.seguridad.entity.BusquedaDTO;
import ig.metrica.ui.interfaces.comunes.IMetodosPantallas;
import ig.util.MyTableModel;
import ig.util.NoContinuaException;
import java.awt.Component;
import java.rmi.RemoteException;
import java.util.Collection;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author  jsanchez
 */
public class DBuscar extends javax.swing.JDialog implements IMetodosPantallas{
    
    MUsuarios modelo=null;
    MyTableModel myTableModel =null;
    /** Creates new form PBsucarUsuario */
//    ManipulacionDatos mpd=null;
    String[] Columnas ={"Login de la Cuenta", "Rol", "Nombre(s) del usuario", "Apellido(s) del usuario"};
    Object [][] Datos=null;
    boolean[] Editables = {false, false, false, false};    
    
    public DBuscar(MUsuarios modelo){
        super(modelo.getRootPanel(),true);        
        myTableModel= new MyTableModel(Datos,Columnas,Editables);
        initComponents();
        
        //this.Tabla.setModel(myTableModel);
//            mpd=new ManipulacionDatos(true,false);
//            mpd.setLimpiarComponentes(false);
//            //mpd.setConfimacioSalir(true,"Desea cerrar la ventana de Geneacion de Escenarios");
//            mpd.setPanelMetodosPantalla(this);
//            mpd.setSize(400,200);
//            mpd.setTitle("Escenarios");
//            mpd.setEstado(CodigoBotones.grupoNuevoSalir);
        this.modelo=modelo;    
        centrar();
        this.setVisible(true);
    }
    
    public void centrar() {
        java.awt.Dimension dimensionScreen = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        java.awt.Rectangle dimensionPantalla = getBounds();
        setLocation((dimensionScreen.width - dimensionPantalla.width) / 2, (dimensionScreen.height - dimensionPantalla.height) / 2);
    }    
    
    
    
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        Tabla = new javax.swing.JTable();
        jToolBar1 = new javax.swing.JToolBar();
        Buscar = new javax.swing.JButton();
        Aceptar = new javax.swing.JButton();
        Cancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setBackground(new java.awt.Color(255, 255, 255));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        Tabla.setModel(this.myTableModel);
        Tabla.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                TablaMouseClicked(evt);
            }
        });

        jScrollPane1.setViewportView(Tabla);

        jToolBar1.setEnabled(false);
        Buscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ig/util/images/buscar.gif")));
        Buscar.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 6, 1, 6));
        Buscar.setOpaque(false);
        Buscar.setPreferredSize(new java.awt.Dimension(43, 43));
        Buscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BuscarActionPerformed(evt);
            }
        });

        jToolBar1.add(Buscar);

        Aceptar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ig/util/images/aceptar.gif")));
        Aceptar.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 6, 1, 6));
        Aceptar.setOpaque(false);
        Aceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AceptarActionPerformed(evt);
            }
        });

        jToolBar1.add(Aceptar);

        Cancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ig/util/images/salir.gif")));
        Cancelar.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 6, 1, 6));
        Cancelar.setOpaque(false);
        Cancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelarActionPerformed(evt);
            }
        });

        jToolBar1.add(Cancelar);

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(12, 12, 12)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 432, Short.MAX_VALUE)
                .addContainerGap())
            .add(jToolBar1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 456, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(jToolBar1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 69, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 280, Short.MAX_VALUE)
                .addContainerGap())
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        modelo.setEstado();        
    }//GEN-LAST:event_formWindowClosing

    private void CancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelarActionPerformed
        try {
            this.salir();
        } catch (NoContinuaException ex) {
            ex.printStackTrace();
        }        
    }//GEN-LAST:event_CancelarActionPerformed

    private void AceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AceptarActionPerformed
        this.aceptar();
    }//GEN-LAST:event_AceptarActionPerformed

    private void BuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BuscarActionPerformed
        try {
            this.buscar();
        } catch (NoContinuaException ex) {
            JOptionPane.showMessageDialog(this,ex.getMessage(),"Mensaje",JOptionPane.OK_OPTION);
        }
    }//GEN-LAST:event_BuscarActionPerformed

    private void TablaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_TablaMouseClicked
        if(evt.getClickCount()==2){//Doble click en la tabla
            this.aceptar();
        }
    }//GEN-LAST:event_TablaMouseClicked
    
//    /**
//     * @param args the command line arguments
//     */
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                new DBuscar(new javax.swing.JFrame(), true).setVisible(true);
//            }
//        });
//    }

    public JInternalFrame getInternalFrame() {
        return null;
    }

    public void validar() throws NoContinuaException {
    }

    public void guardar() throws NoContinuaException {
    }
    //String[] Columnas ={"Usuario", "Tipo", "Nombres", "Apellidos"};
    private Collection CBusqueda=null;
    public void buscar() throws NoContinuaException {
        try {
            CBusqueda=this.modelo.lookupCuentaUsuarioControlBean().findAllCuentas(modelo.getLogin());
            Datos= new Object[CBusqueda.size()][4];
            int i=0;
            for (Object elem : CBusqueda) {
                Datos[i][0]=((BusquedaDTO)elem).getUsuario();
                Datos[i][1]=((BusquedaDTO)elem).getRol();
                Datos[i][2]=((BusquedaDTO)elem).getNomres();
                Datos[i][3]=((BusquedaDTO)elem).getApellidos();
                i++;
            }
            ((MyTableModel)this.Tabla.getModel()).setData(Datos);
            this.Tabla.updateUI();
        } catch (RemoteException ex) {
          throw new  NoContinuaException("No hay Conexi\u00F3n con el Servidor");
        } 
    }

    public void modificar() throws NoContinuaException {
    }

    public void nuevo() throws NoContinuaException {
    }

    public void eliminar() throws NoContinuaException {
    }

    public void salir() throws NoContinuaException {
        this.dispose();
        modelo.setEstado();
    }

    public void imprimir() throws NoContinuaException {
    }

    public void cancelar() throws NoContinuaException {
    }

    public Component[] getControles() {
        return null;
    }

    public void setFrame(JFrame frame) {
    }

    public JFrame getFrame() {
        return null;
    }

    private void aceptar() {
        if(CBusqueda!=null){            
            //System.out.println(this.Tabla.getSelectedRow());
            if(this.Tabla.getSelectedRow()!=-1){
                modelo.setBusquedaDTO((BusquedaDTO)CBusqueda.toArray()[this.Tabla.getSelectedRow()]);
                this.dispose();
            }else
                JOptionPane.showMessageDialog(this,"No se ha seleccionado ninguna cuenta","Mensaje",JOptionPane.OK_OPTION);
                
        }
    }
    
//     private ServiceLocator serviceLocator;
//     private ServiceLocator getServiceLocator() {
//        if (serviceLocator == null) {
//            serviceLocator = new ServiceLocator();
//        }
//        return serviceLocator;
//    }
//     
//   private ejb.metrica.seguridad.control.CuentaUsuarioControlRemote lookupCuentaUsuarioControlBean() {
//        try {
//            return ((ejb.metrica.seguridad.control.CuentaUsuarioControlRemoteHome) getServiceLocator().getRemoteHome("CuentaUsuarioControlBean",ejb.metrica.seguridad.control.CuentaUsuarioControlRemoteHome.class)).create();
//        } catch(javax.naming.NamingException ne) {
//            java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,ne);
//            throw new RuntimeException(ne);
//        } catch(javax.ejb.CreateException ce) {
//            java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,ce);
//            throw new RuntimeException(ce);
//        } catch(java.rmi.RemoteException re) {
//            java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,re);
//            throw new RuntimeException(re);
//        }
//    }    
   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Aceptar;
    private javax.swing.JButton Buscar;
    private javax.swing.JButton Cancelar;
    private javax.swing.JTable Tabla;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar jToolBar1;
    // End of variables declaration//GEN-END:variables
    
}
